package server;
/**
 * most important file
 * @author Jaak-Alo Lukanenok
 * @author Taavi Rehem�gi
 */
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.StringTokenizer;

import client.HTTPClient;

import shared.Contacts;
import shared.MyData;
import shared.User;

class httpRequestHandler {
  final static String CRLF = "\r\n";

	public void handleRequest(String inputRead){
		
		HTTPClient client=new HTTPClient();
		/*
		String inputRead="";
		try {
			inputRead=input.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}
		*/
		if(inputRead.contains("/chat/findname?name=")){
			String name = inputRead.substring( inputRead.indexOf("?name=") + 6, inputRead.lastIndexOf("&myip=") );
			
			
			
			String ipPort = inputRead.substring( inputRead.lastIndexOf("&myip=") + 6, inputRead.lastIndexOf("&ttl=") );

			String[] ip=ipPort.split(":");
/*
			System.out.println("Hakkan võrldema");
			System.out.println("Nimed on: "+name+" ja "+MyData.getName());
			System.out.println("Ipd on: "+ip[0]+" ja "+MyData.getIP());
			System.out.println("validate on: "+MyData.validate);
			*/
			if(name.equals(MyData.getName()) && ip[0].equals(MyData.getIP())){
				System.out.println("Valitud nimi on vaba! sisesta uuesti see nimi, et salvestada!");
				MyData.validate=1;
				return;
				
			}
			else if(name.equals(MyData.getName()) && (!(ip[0].equals(MyData.getIP()))) && MyData.validate==1){
				
				//System.out.println("Saan aru, et nimi on kasutusel minu poolt! ning saadan tagasi teate, et ou mees see on kasutusel, saada pordile: "+ipPort );
				client.findName(name, ipPort, MyData.getIpPort());
			}
			else if(name.equals(MyData.getName()) && (!(ip[0].equals(MyData.getIP()))) && (MyData.validate==0 || MyData.validate==2)){
				//System.out.println("Siin avastan, et mulle öeldi, et see nimi on kasutusel!");
				MyData.validate=2;
			}
			
			
			else{
				//System.out.println("Saan aru, et küsitakse kas kellelgi on selline nimi, minul ei ole ja hakkan võrdlema kontaktidega.");
				Contacts contacts=new Contacts();
				User user=null;
				user=contacts.getContact(name);
				if(user==null){
					//System.out.println("See nimi ei ole kasutusel!!!");
					client.findName(name, ipPort,ipPort);
					
				}
					
				else{
					//System.out.println("See nimi on kasutusel!!!");
					client.findName(user.getName(), user.getIP()+":"+user.getPort(),user.getIP()+":"+user.getPort());
					
				}								
			}	
			
		}
		else if(inputRead.contains("/chat/sendmessage?myname=")) {
			String name = inputRead.substring( inputRead.indexOf("?myname=") + 8, inputRead.lastIndexOf("&myip=") );
			String ip_port = inputRead.substring( inputRead.lastIndexOf("&myip=") + 6, inputRead.lastIndexOf("&message=") );
			String message = inputRead.substring( inputRead.lastIndexOf("&message=") + 9, inputRead.lastIndexOf("&ttl="));
			System.out.println(name+" says: "+message);
			
		}
		else if(inputRead.contains("/chat/sendname?name=")){
			//System.out.println("Keegi tahab mulle enda nime saata");
			String name = inputRead.substring( inputRead.indexOf("sendname?name=") + 14, inputRead.lastIndexOf("&myip=") );
			String ip_port = inputRead.substring( inputRead.lastIndexOf("&myip=") + 6, inputRead.lastIndexOf("&ttl=") );
			String[] ip=ip_port.split(":");
			Contacts contacts=new Contacts();
			User test=contacts.getContact(name);
			
			if(test==null){

				//System.out.println("Salvestan nime: "+name+" "+ip[0]+":"+ip[1]);
				User user=new User(name,ip[0],ip[1]);
				Contacts.contacts.add(user);
				client.sendName(MyData.name, ip[0]+":"+ip[1]);
				
			}
			
			
		}
		// /chat/sendmessage?myname=" + MyData.getName() + "&myip=" + MyData.getIP() + ":" + MyData.getPort() + "&message=" + message + "&ttl=0"
		else if(inputRead.contains("/chat/sendmessage")){
			String name = inputRead.substring( inputRead.indexOf("?myname=") + 8, inputRead.lastIndexOf("&myip=") );
			String message = inputRead.substring( inputRead.lastIndexOf("&message=") + 9, inputRead.lastIndexOf("&ttl=") );
			System.out.println("Chat "+name+":"+message);
			
		}
	}
	
	

  
}